/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#ifndef CAMERA_H
#define CAMERA_H

#include "Object.h"
#define GIMI_CLIENT_API 20000
#include "gimi.h"
#undef GIMI_CLIENT_API
#include "GL/gl.h"
#include "Shapes.h"

#include "JointGroupCtrl/JointGroupCtrlServer.hpp"
#include "JointGroupCtrl/JointGroupCtrlTypes.hpp"
#include "Image/ImageTypes.hpp"
#include "Image/ImageContainer.hpp"
#include "Image/ImageServer.hpp"

class CCamera : public CObject
{
  public:
    CCamera(CWorld *World, CObject *ParentObject, dBodyID parentBody,
            dReal *posOffset, dMatrix3 rotOffset, unsigned int resoX,
            unsigned int resoY, unsigned int fovy, gimi::GIMI* gimi,
            MaCI::MaCICtrl::CMaCICtrlServer *mcs, std::string cameraInterfaceName="CameraFront",
            std::string PTUInterfaceName="ServoCtrl",
            bool noiseMode=false);//,
            //bool stereo=false, float distance=0);

    ~CCamera();
    
    void Activate();
    void Deactivate();

    void Update(float StepSize);

    void Draw();

    void OnCollision(CPrimitive *primitive, int numc, dContact *contacts, CPrimitive *primitive2);
  protected:
    //set this to true when rendering in camera context so that other cameras
    //called wont start rendering (endless recursion)
    static bool RenderingForCamera;

  private:
    dReal *PosOffset;
    dMatrix3 RotOffset;

    unsigned int resoX, resoY;
    unsigned int fovy; //field of view in y direction
    unsigned int fps; //frames per second
    float timer;

    int minorType;
    gimi::GIMI* gimi;
    MaCI::MaCICtrl::CMaCICtrlServer *mcs;
    gim::binbag::CBinBag binbag;
    MaCI::JointGroupCtrl::CJointGroupCtrlServer jointServer;
    MaCI::Image::CImageServer imageServer;
    MaCI::Image::CImageData imageData;
    float pan;
    float tilt;
    
/*    bool stereo;
    float distance; //distance between cameras if stereo
*/
    
    bool noiseMode;
    
    CBox *box;

    bool glInited;
    bool glOK;
    
    int contextID;
    
    bool errorImageLoaded;
    unsigned char* errorImageJPGData;
    int errorDataSize;
    
    unsigned char *rgbData;
    unsigned char *rgbDataFlipped;
    
    void initGL();
    void initGimi();

    void sendImage(unsigned char *data, int size);

    void sendErrorImage();

    void setPanAndTilt();//float pan, float tilt);
};

#endif
